package com.atguigu.mybatisplusdemo.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * 认证配置
 */
@Configuration
@RequiredArgsConstructor
class AuthenticationConfig {
//    //对象映射器()
//    private final ObjectMapper objectMapper;
//    private final JwtTokenService jwtTokenService;
//    private final CustomAuthenticationProvider customAuthProvider;
//
//
//    void configure(HttpSecurity http) throws Exception {
//        // 添加自定义登录过滤器
//        http.addFilterBefore(
//                new CustomLoginFilter(
//                        objectMapper,
//                        authManager(http),
//                        authenticationSuccessHandler(),
//                        authenticationFailureHandler()
//                ),
//                UsernamePasswordAuthenticationFilter.class
//        );
//    }
//
//    /**
//     * 登录成功处理器
//     */
//    @Bean
//    AuthenticationSuccessHandler authenticationSuccessHandler() {
//        return (request, response, authentication) -> {
//            response.setStatus(HttpStatus.OK.value());
//            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
//            String jwt = jwtTokenService.generateJwtToken(authentication);
//            R r = R.builder().code(200).msg("登录成功").info(jwt).build();
//            objectMapper.writeValue(response.getWriter(), r);
//        };
//    }
//
//    /**
//     * 登录失败处理器
//     */
//    @Bean
//    AuthenticationFailureHandler authenticationFailureHandler() {
//        return (request, response, exception) -> {
//            response.setStatus(HttpStatus.UNAUTHORIZED.value());
//            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
//            response.setCharacterEncoding("UTF-8");
//
//            String errorMessage = switch (exception) {
//                case BadCredentialsException e -> "用户名或密码错误";
//                case AuthenticationServiceException e -> "登陆服务异常: " + e.getMessage();
//                default -> "认证失败";
//            };
//
//            int errorCode = (exception instanceof BadCredentialsException ||
//                    exception instanceof AuthenticationServiceException) ? 401 : 500;
//
//            R r = R.builder().code(errorCode).msg(errorMessage).build();
//            objectMapper.writeValue(response.getWriter(), r);
//        };
//    }
//
//    /**
//     * 身份验证管理器，用于匹配AuthenticationProvider,进行身份验证
//     */
//    @Bean
//    public AuthenticationManager authManager(HttpSecurity http) throws Exception {
//        return http.getSharedObject(AuthenticationManagerBuilder.class)
//                .authenticationProvider(customAuthProvider)
//                .build();
//    }
 
}